﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;

namespace ThousandFunClient
{
    public partial class LinqXML 
    {


        /// <summary>
        /// 绑定数据
        /// </summary>
        public void SetBind()
        {
            List<Student> list = GetData(20);
            CreateFile(@"c:/students.xml", "students", list);
            
            //AddXmlElement(@"c:/students.xml");
            
            //UpdateXmlElement(@"c:/students.xml",""+104);
            // RemoveXmlElement(@"c:/students.xml");
            //ConvertXmlAttributeToElement(@"c:/students.xml");
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public List<Student> GetData(int n)
        {
            List<Student> list = new List<Student>();
            Student s;
            for (int i = 0; i < n; i++)
            {

                if (i % 2 == 0)
                {
                    s = new Student(i * 10 + 40, "nihao" + i, "男");
                }
                else
                {
                    s = new Student(i * 10 + 40, "nihao" + i, "女");
                }
                list.Add(s);
            }

            return list;

        }

        /// <summary>
        /// 使用linq 建立xml
        /// </summary>
        /// <param name="filePath">路径</param>
        /// <param name="rootElement">根元素</param>
        /// <param name="list">对象</param>
        public void CreateFile(string filePath, string rootElement, List<Student> list)
        {
            if (File.Exists(filePath)) //文件存在就删除
            {
                FileInfo file = new FileInfo(filePath);
                file.Delete();
            }
            XElement contacts = new XElement(rootElement);
            foreach (Student s in list)
            {
                XElement el = new XElement("student", new XAttribute("number", s.Number),
                    new XElement("name", s.Name),
                    new XElement("sex", s.Sex)
                    );

                contacts.Add(el);
            }
            contacts.Save(filePath);
        }

        /// <summary>
        /// //增加元素到XML文件
        /// </summary>
        /// <param name="filename">文件名</param>
        private void AddXmlElement(string filename)
        {
            ///导入XML文件
            XElement xe = XElement.Load(filename);
            ///创建一个新的节点
            XElement student = new XElement("student",
             new XAttribute("number", "104"),                    ///添加属性number
             new XElement("name", "chenxt"),                     ///添加元素Name
             new XElement("sex", "男")                           ///添加元素sex
             );
            ///添加节点到文件中，并保存
            xe.Add(student);
            xe.Save(filename);

        }

        /// <summary>
        /// //修改XML文件中的元素
        /// </summary>
        /// <param name="filePath">路径</param>
        /// <param name="number">number</param>
        private void UpdateXmlElement(string filePath, string number)
        { ///导入XML文件

            XElement xe = XElement.Load(filePath);
            
            ///查找被替换的元素
            IEnumerable<XElement> element = from e in xe.Elements("student")
                                            where e.Attribute("number").Value == "104"
                                            select e;

            
            //IEnumerable<XElement> element = from e in xe.Elements("student")
            //                                where e.Element("sex").Value=="女"
            //                                select e;

            // XElement x= element.ElementAt(0);

            ///替换为新元素，并保存
            if (element.Count() > 0)
            {
                XElement first = element.First();
                ///设置新的属性
                first.SetAttributeValue("number", "106");
                ///替换新的节点
                first.ReplaceNodes(
                 new XElement("Name", "陈宪涛"),              ///添加元素Name
                 new XElement("sex", "男")                   ///添加元素Price
                 );
            }
            xe.Save(filePath);

        }
        /// <summary>
        /// 删除元素
        /// </summary>
        /// <param name="filePath"></param>
        private void RemoveXmlElement(string filePath)//删除XML文件中的元素
        {
            ///导入XML文件
            XElement xe = XElement.Load(filePath);
            ///查找被删除的元素
            IEnumerable<XElement> element = from e in xe.Elements()
                                            where (string)e.Element("name") == "nihao1"
                                            select e;
            ///删除指定的元素，并保存
            if (element.Count() > 0)
            {
                element.First().Remove();
            }
            xe.Save(filePath);

        }
        /// <summary>
        /// 将属性名改为节点名
        /// </summary>
        /// <param name="filePath">路径名</param>
        private void ConvertXmlAttributeToElement(string filePath)//将XML文件中的属性转换为元素
        { ///导入XML文件

            XElement xe = XElement.Load(filePath);
            ///查找被替换的元素
            IEnumerable<XElement> element = from e in xe.Elements("student")
                                            where e.Attribute("number").Value == "106"
                                            select e;
            ///替换为新元素，并保存
            if (element.Count() > 0)
            {
                XElement first = element.First();
                ///获取第一个属性
                XAttribute attribute = first.FirstAttribute;
                ///将属性转换为元素
                first.AddFirst(
                 new XElement(attribute.Name, attribute.Value) ///添加元素ID
                 );
                ///删除属性
                first.RemoveAttributes();
            }
            xe.Save(filePath);
   
        }

        
    }

    public class Student
    {

        private int number;

        private string name;

        private string sex;

        //学号 独一无二
        public int Number
        {
            set
            {
                number = value;
            }
            get
            {
                return number;
            }
        }

        //姓名
        public string Name
        {

            set
            {
                name = value;
            }
            get
            {
                return name;
            }

        }

        //性别
        public string Sex
        {
            set
            {
                sex = value;
            }
            get
            {
                return sex;
            }
        }
        public Student(int number, string name, string sex)
        {
            this.name = name;
            this.sex = sex;
            this.number = number;

        }

    }
}
